package com.turbo.dialog;

import com.intellij.ide.IdeBundle;
import com.intellij.ide.IdeView;
import com.intellij.ide.util.BrowseFilesListener;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.application.ApplicationNamesInfo;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.fileChooser.FileTypeDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.ui.messages.MessageDialog;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.ui.Gray;
import com.turbo.service.impl.ProjectServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author: zwx
 * @time: 2021/3/22 21:35
 */
public class ReadDialog extends DialogWrapper {
    private AnActionEvent anActionEvent;

    private JTextField tfModuleFilePath;
    private String path;

    public ReadDialog(AnActionEvent anActionEvent){
        super(true);
        init();
        setCancelButtonText("取消");
        setOKButtonText("确定");
        setTitle("生成枚举类");
        this.anActionEvent = anActionEvent;
    }

    @Override
    protected JComponent createCenterPanel() {
        // 创建一个1行1列的面板
        JPanel jPanel = new JPanel(new GridLayout(1,1));

        // 创建第一行的布局 一个lable，一个文件选择器
        JLabel jLabel = new JLabel("选择yml文件：");
        jLabel.setPreferredSize(new Dimension(80,30));
        tfModuleFilePath = new JTextField();
        tfModuleFilePath.setMaximumSize(new Dimension(400,30));
        tfModuleFilePath.setMinimumSize(new Dimension(400,30));
        tfModuleFilePath.setPreferredSize(new Dimension(400,30));
        final BrowseFilesListener listener = new BrowseFilesListener(tfModuleFilePath, "选择文件", null, new FileTypeDescriptor("yml文件","yml", "yaml")) {
            @Override
            protected @Nullable VirtualFile getFileToSelect() {
                return super.getFileToSelect();
            }
            @Override
            protected void doSetText(@NotNull String path) {
                super.doSetText(path);
            }
        };
        TextFieldWithBrowseButton button = new TextFieldWithBrowseButton(tfModuleFilePath, listener);

        // 获取水平box，将label放到该水平box布局的最左边，button放到该水平box布局的中间  createHorizontalBox为水平箱子，相对的还有垂直布局的箱子
        Box hoBox = Box.createHorizontalBox();
        hoBox.add(jLabel, BorderLayout.WEST);
        hoBox.add(button, BorderLayout.CENTER);

        // 创建一个label，设置字体大小颜色，将其放到水平box中
        JLabel jLabel1 = new JLabel("可选择yml、yaml格式的文件。请确保内容正确，否则生成过程中会失败。");
        Font font = new Font(null, 0, 11);
        jLabel1.setFont(font);
        jLabel1.setForeground(Gray._128);
        // 设置文字对其方式
        jLabel1.setHorizontalAlignment(SwingConstants.LEFT);

        Box hoBox1 = Box.createHorizontalBox();
        hoBox1.add(jLabel1);

        // 创建一个垂直box，将两个水平box依次放到垂直box中，最后将垂直box放到面板上
        Box vBox = Box.createVerticalBox();
        vBox.add(hoBox);
        vBox.add(hoBox1);
        jPanel.add(vBox);
        // FlowLayout 用于组件对其方式
        jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        // 直接打开文件选择器
//        FileTypeDescriptor fileTypeDescriptor = new FileTypeDescriptor("yml文件", "yml");
//        VirtualFile virtualFile = FileChooser.chooseFile(fileTypeDescriptor, project, null);
//        path = virtualFile.getPath();

        return jPanel;
    }

    @Override
    protected void doOKAction() {
        super.doOKAction();
        IdeView ideView = anActionEvent.getData(LangDataKeys.IDE_VIEW);
        PsiDirectory directory = ideView.getOrChooseDirectory();
        boolean bool = new ProjectServiceImpl().readYml(anActionEvent.getProject(), tfModuleFilePath.getText(), directory.toString());
        if (!bool){
            Messages.showErrorDialog("生成过程中出现未知错误! 可能文件内容不正确或者所选目录违法", "错误");
        }
        // new ProjectServiceImpl().readYml(project, path);
    }


}
